import pyecharts.options as opts
from pyecharts.charts import WordCloud
import pandas as pd
import jieba
from pyecharts.render import make_snapshot
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://gallery.echartsjs.com/editor.html?c=xS1jMxuOVm

目前无法实现的功能:

1、暂无
"""
pd.read_excel("/Users/c/存档/2021-12-01人行征信库/0101公司代码/xfeature-yh-gen2/xfeature-yh/doc/人行二代-特征字典-V1.0.0.xlsx")

xl = pd.ExcelFile("/Users/c/存档/2021-12-01人行征信库/0101公司代码/xfeature-yh-gen2/xfeature-yh/doc/人行二代-特征字典-V1.0.0.xlsx")
print(xl)
print(xl.sheet_names)  # 所有的sheet名称

result=[]
print(result)

#拿到所有的特征解释
for sheet_name in xl.sheet_names[1:]:
    print(sheet_name)
    result = result+list(xl.parse(sheet_name)['特征解释'].dropna(axis=0,how='all'))  # 读取Excel中sheet_name的数据

print(len(result))
column_dict={}
#分词
for column in result:
    seg_list = jieba.cut(column, cut_all=False)
    #print(column)
    #print(list(seg_list))  # 精确模式
    #分词结果填充到dict对象，
    for seg in seg_list:
        if seg in column_dict:
            column_dict[seg]=column_dict[seg]+1
        else:
            column_dict[seg] =1

del column_dict['_']
del column_dict['(']
del column_dict[')']
del column_dict['（']
del column_dict['）']
del column_dict['【']
del column_dict['】']
del column_dict['|']
del column_dict['｜']
del column_dict['-']
del column_dict['__']
column_dict_list = sorted(column_dict.items(),key = lambda x:x[1],reverse = True)

print(column_dict_list)
#dict转list
# lst_key=[]
# for key,value in column_dict.items():
#     lst_key=lst_key+[(key,value)]
# print (lst_key)

# data = [
#     ("生活资源", "999"),
#     ("供热管理", "888")
# ]




(
    WordCloud(init_opts=opts.InitOpts(width='1000px', height='1000px'))
    .add(series_name="", data_pair=column_dict_list, word_size_range=[15, 66])
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
        ),
        tooltip_opts=opts.TooltipOpts(is_show=True),
    )
    .render("basic_wordcloud.html")
)


#距证图
import json

from pyecharts import options as opts
from pyecharts.charts import TreeMap
print(column_dict)
c = (
    TreeMap()
    .add(
        series_name="",
        data=column_dict,
        levels=[
            opts.TreeMapLevelsOpts(
                treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
                    border_color="#555", border_width=4, gap_width=4
                )
            ),
            opts.TreeMapLevelsOpts(
                color_saturation=[0.3, 0.6],
                treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
                    border_color_saturation=0.7, gap_width=2, border_width=2
                ),
            ),
            opts.TreeMapLevelsOpts(
                color_saturation=[0.3, 0.5],
                treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
                    border_color_saturation=0.6, gap_width=1
                ),
            ),
            opts.TreeMapLevelsOpts(color_saturation=[0.3, 0.5]),
        ],
    )
    .set_global_opts(title_opts=opts.TitleOpts(title="TreeMap-Levels-配置"))
    .render("treemap_levels.html")
)